package com.niannian.util;

import java.io.File;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.niannian.MyApplication;
import com.niannian.bean.DownloadItemBean;
import com.niannian.dialog.CheckVersionDialog;
import com.niannian.dialog.CheckVersionDialog.IClickCallback;
import com.niannian.service.UpdateVersionService;
import com.niannian.web.nnSyncApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

public abstract class Version extends AsyncHandle {
	private Context context;
	private String localVersion;
	private String version;
	private String desc;
	private String downloadurl;
	private String datetime;
	private int filesize;
	private CheckVersionDialog tipdialog;
	private String savePath = MyApplication.FILE_PATH_TEMP + "niannian.apk";
	private boolean downloadImmediately = false;
	private boolean showTips = false;
	private boolean isshowVerD = true;

	private int versionState = 0;// 版本状态 1正在检查,2已是最新版本,3正在下载最新版本,4有新版本

	public Version(Context context, boolean downloadImmediately, boolean showtips) {
		this.context = context;
		this.localVersion = SpocketHelper.getPackageVersion(context);
		this.downloadImmediately = downloadImmediately;
		this.showTips = showtips;

	}

	public Version(Context context, boolean downloadImmediately, boolean showtips, boolean isshowVerD) {
		this.context = context;
		this.localVersion = SpocketHelper.getPackageVersion(context);
		this.downloadImmediately = downloadImmediately;
		this.showTips = showtips;
		this.isshowVerD = isshowVerD;
	}

	abstract public void showLatestVersion(String latestVersion, String desc, String downloadurl, String datetime);

	abstract public void showVersionState(int versionState);

	public void showDownloadDialog() {
		tipdialog = new CheckVersionDialog(context);
		tipdialog.setTitle("发现新版本: " + version);
		tipdialog.setMsg(desc);
		final File file = new File(savePath);
		if (file.exists()) {
			tipdialog.setConfimButtonText("立即安装");
		} else {
			tipdialog.setConfimButtonText("立即更新");
		}

		tipdialog.setClickCallback(new IClickCallback() {
			@Override
			public void onCancel() {
				tipdialog.dismiss();
			}

			@Override
			public void onConfirm() {
				if (file.exists()) {
					// Common.tip(context, "需要卸载旧版本");
					SpocketHelper.openApk(file, context);
					// SpocketHelper.delApk(context);
					tipdialog.dismiss();
					return;
				}
				showVersionState(3);
				DownloadItemBean download = new DownloadItemBean();
				download.title = "咚咚" + version + "版本升级";
				download.save_path = savePath;
				download.total_size = filesize;
				download.url = downloadurl;
				Intent intent = new Intent(context, UpdateVersionService.class);
				intent.putExtra("download", download);
				context.startService(intent);
				tipdialog.dismiss();
			}

			@Override
			public void onNeglect() {
				SharedPreferences versionInfo = context.getSharedPreferences("version_info", 0);
				versionInfo.edit().putString("version_info", version).commit();
				tipdialog.setNeglectBackgound(true);
				tipdialog.dismiss();
			}
		});
		tipdialog.show();
	}

	@Override
	protected void handleData(JSONObject data, Map<String, Object> parameters) throws JSONException {
		try {
			version = data.getString("version");
			if (!version.equals(localVersion)) {
				version = data.getString("version");
				desc = data.getString("changelog");
				// downloadurl = Common.APK_DOWNLOAD_BASE_URL;
				downloadurl = data.getString("update_url");
				datetime = "";
				filesize = data.getInt("pkg_size");
				savePath = MyApplication.FILE_PATH_TEMP + "niannian." + version + ".apk";
				showVersionState(4);
				showDownloadDialog();
			} else {
				showVersionState(2);
				if (showTips)
					Common.tip(context, "已经是最新版本");
			}

		} catch (Exception e) {
		}
	}

	@Override
	protected String runTask(Map<String, Object> parameters) {
		return nnSyncApi.checkVersion();
	}

}
